package com.cjntaylor.termpanel;

import java.io.File;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import e.util.Log;
import e.gui.AboutBox;
import e.gui.MenuItemProvider;
import terminator.TerminatorPreferences;
import terminator.terminal.TerminalControl;
import terminator.view.JTerminalPane;
import terminator.view.TerminalPaneHost;
import terminator.view.TerminatorMenuItemProvider;

public class TerminalPanel extends JTerminalPane {
	
	private static int event = 0;
	
	protected static File prefFile;
	protected static final TerminatorPreferences pref = new TerminatorPreferences();
	{ 
		try {
			prefFile = new File(pref.getPreferencesFilename());
			if(!prefFile.exists()) {
				pref.initPreferences();
				pref.writeToDisk(); 
			}	
		} catch(Exception ex) { ex.printStackTrace(); }
	}
	
	public static void main(String[] args) {
		// Create the jframe for the terminal panel
		Log.setApplicationName("TerminalPanel");
		JFrame frame = new JFrame("TerminalPanel");
		
		// Modify about box to include both copyrights
		AboutBox aboutBox = AboutBox.getSharedInstance();
		aboutBox.addCopyright("Copyright (C) 2011, Colin Taylor");
		aboutBox.addCopyright("   ");
		aboutBox.addCopyright("Terminator/Salma-Hayek:");
		
		// Create the terminal panel, bind exit to close
		TerminalPanel panel = new TerminalPanel();
		panel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		// Modify the website to point to my server
		aboutBox.setWebSiteAddress("http://termpanel.cjntaylor.com/");

		// Show the terminal panel
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(700, 400);
		frame.setVisible(true);	
	}
	
	public TerminalPanel() {
		super(System.getProperty("user.name") + "@localhost", null, 
		TerminalControl.getDefaultShell(), true);
		
		this.start(new TerminalPaneHost(){
			public void cycleTab(int delta) {}
            public void moveCurrentTab(int direction) {}
            public void setSelectedTabIndex(int index) {}
            public boolean confirmClose(String processesUsingTty) {return true;}
            public MenuItemProvider createMenuItemProvider(JTerminalPane terminalPane) {
                return new TerminatorMenuItemProvider(terminalPane);
            }
            public void updateFrameTitle() {}
            public void terminalNameChanged(JTerminalPane terminalPane) {}
            public void setTerminalSize(Dimension size) {}
            public void closeTerminalPane(JTerminalPane terminalPane) {
				TerminalPanel.this.fireCloseEvent();
			}
		});
	}
	
	public void addActionListener(ActionListener l) {
		this.listenerList.add(ActionListener.class, l);
	}
	
	private void fireCloseEvent() {
        List<ActionListener> list = Arrays.asList(
        this.listenerList.getListeners(ActionListener.class));
        Collections.reverse(list); 
        for(ActionListener a : list.toArray(new ActionListener[]{})) {
            a.actionPerformed(new ActionEvent(this, TerminalPanel.event++, 
            "close"));
        }
    }
}
